package com.dk.controller;

import com.dk.RES;
import com.dk.service.OrderService;
import com.dk.vo.order.OrderInfo;
import com.dk.vo.order.OrderReceive;
import com.dk.vo.order.OrderShow;
import com.jack.weChatSecurity.core.SecurityHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Controller
@RequestMapping("/api")
public class OrderController {

    @Autowired
    OrderService orderService;

    @GetMapping("/orders")
    @ResponseBody
    public RES getOrderShows(@RequestParam(value = "byUser",defaultValue = "false")boolean byUser, @RequestParam(value = "state",defaultValue = "0")int state){
        RES res=new RES();
        System.out.println("byUser:"+byUser);
        System.out.println("state:"+state);
        try{
            List<OrderShow> orderShows=null;
            if(byUser){
                String uid=SecurityHelper.getWeChatUser().getId();
                orderShows = orderService.getOrdersByUid(uid,state);
            }else{
                orderShows=orderService.getAllOrders();
            }
            res.setCode(0)
                    .setMsg("ok")
                    .addData("orders",orderShows);
        }catch (Exception e){
            e.printStackTrace();
            res.setCode(1).setMsg("error");
        }
        return res;
    }

    @PostMapping("/createOrder")
    @ResponseBody
    public RES createOrder(@RequestBody OrderReceive order){
        RES res=new RES();
        try {
            orderService.saveOrder(order);
            res.setCode(1)
                    .setMsg("发布成功");
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(0).setMsg("发布失败");
        }
        return res;
    }

    @GetMapping("/order")
    @ResponseBody
    public RES getOrderInfo(@RequestParam("orderId")String orderId){
        RES res=new RES();
        System.out.println(orderId);
        try{
            OrderInfo orderInfo=null;
            if(orderId!=null){
                orderInfo=orderService.getOrder(orderId);
            }
            res.setCode(0).setMsg("ok")
                    .addData("order",orderInfo);
        }catch (Exception e){
            e.printStackTrace();
            res.setCode(1).setMsg("error");
        }finally {
            return res;
        }
    }

    @GetMapping("/receiveOrder")
    @ResponseBody
    public RES receiveOrder(@RequestParam("orderId")String orderId){
        RES res=new RES();
        try{
            orderService.receiveOrder(orderId);
            res.setCode(0).setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            res.setCode(1).setMsg("error");
        }finally {
            return res;
        }
    }

    @GetMapping("/finishOrder")
    @ResponseBody
    public RES finishOrder(@RequestParam("orderId")String orderId){
        RES res=new RES();
        try{
            orderService.finishOrder(orderId);
            res.setCode(0).setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            res.setCode(1).setMsg("error");
        }finally {
            return res;
        }
    }

    @GetMapping("/checkOrder")
    @ResponseBody
    public RES checkOrder(@RequestParam("orderId")String orderId){
        RES res=new RES();
        try{
            orderService.checkOrder(orderId);
            res.setCode(0).setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            res.setCode(1).setMsg("error");
        }finally {
            return res;
        }
    }

    @GetMapping("/orderRecord")
    @ResponseBody
    public RES orderRecord(){
        RES res=new RES();
        try{
            String uid=SecurityHelper.getWeChatUser().getId();
            List<OrderShow> orders=orderService.getOrdersByUid(uid,3);
        }catch (Exception e){

        }finally {
            return res;
        }
    }
}
